<!DOCTYPE html>
<html lang="en-us">
  <head>

    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    
<meta charset="UTF-8">
<title>Rare Terms Aggregation | Elasticsearch Guide [7.7] | Elastic</title>
<link rel="home" href="index.html" title="Elasticsearch Guide [7.7]">
<link rel="up" href="search-aggregations-bucket.html" title="Bucket Aggregations">
<link rel="prev" href="search-aggregations-bucket-range-aggregation.html" title="Range Aggregation">
<link rel="next" href="search-aggregations-bucket-reverse-nested-aggregation.html" title="Reverse nested Aggregation">
<meta name="DC.type" content="Learn/Docs/Elasticsearch/Reference/7.7">
<meta name="DC.subject" content="Elasticsearch">
<meta name="DC.identifier" content="7.7">
<meta name="robots" content="noindex,nofollow">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://cdn.optimizely.com/js/18132920325.js"></script>
    <link rel="apple-touch-icon" sizes="57x57" href="/apple-icon-57x57.png">
    <link rel="apple-touch-icon" sizes="60x60" href="/apple-icon-60x60.png">
    <link rel="apple-touch-icon" sizes="72x72" href="/apple-icon-72x72.png">
    <link rel="apple-touch-icon" sizes="76x76" href="/apple-icon-76x76.png">
    <link rel="apple-touch-icon" sizes="114x114" href="/apple-icon-114x114.png">
    <link rel="apple-touch-icon" sizes="120x120" href="/apple-icon-120x120.png">
    <link rel="apple-touch-icon" sizes="144x144" href="/apple-icon-144x144.png">
    <link rel="apple-touch-icon" sizes="152x152" href="/apple-icon-152x152.png">
    <link rel="apple-touch-icon" sizes="180x180" href="/apple-icon-180x180.png">
    <link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32">
    <link rel="icon" type="image/png" href="/android-chrome-192x192.png" sizes="192x192">
    <link rel="icon" type="image/png" href="/favicon-96x96.png" sizes="96x96">
    <link rel="icon" type="image/png" href="/favicon-16x16.png" sizes="16x16">
    <link rel="manifest" href="/manifest.json">
    <meta name="apple-mobile-web-app-title" content="Elastic">
    <meta name="application-name" content="Elastic">
    <meta name="msapplication-TileColor" content="#ffffff">
    <meta name="msapplication-TileImage" content="/mstile-144x144.png">
    <meta name="theme-color" content="#ffffff">
    <meta name="naver-site-verification" content="936882c1853b701b3cef3721758d80535413dbfd">
    <meta name="yandex-verification" content="d8a47e95d0972434">
    <meta name="localized" content="true">
    <meta name="st:robots" content="follow,index">
    <meta property="og:image" content="https://www.elastic.co/static/images/elastic-logo-200.png">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <link rel="icon" href="/favicon.ico" type="image/x-icon">
    <link rel="apple-touch-icon-precomposed" sizes="64x64" href="/favicon_64x64_16bit.png">
    <link rel="apple-touch-icon-precomposed" sizes="32x32" href="/favicon_32x32.png">
    <link rel="apple-touch-icon-precomposed" sizes="16x16" href="/favicon_16x16.png">
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <link rel="stylesheet" type="text/css" href="/guide/static/styles.css">
  </head>

  <!--© 2015-2021 Elasticsearch B.V. Copying, publishing and/or distributing without written permission is strictly prohibited.-->

  <body>
    <!-- Google Tag Manager -->
    <script>dataLayer = [];</script><noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-58RLH5" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
    <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= '//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','GTM-58RLH5');</script>
    <!-- End Google Tag Manager -->

    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-12395217-16"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
      gtag('config', 'UA-12395217-16');
    </script>

    <!--BEGIN QUALTRICS WEBSITE FEEDBACK SNIPPET-->
    <script type="text/javascript">
      (function(){var g=function(e,h,f,g){
      this.get=function(a){for(var a=a+"=",c=document.cookie.split(";"),b=0,e=c.length;b<e;b++){for(var d=c[b];" "==d.charAt(0);)d=d.substring(1,d.length);if(0==d.indexOf(a))return d.substring(a.length,d.length)}return null};
      this.set=function(a,c){var b="",b=new Date;b.setTime(b.getTime()+6048E5);b="; expires="+b.toGMTString();document.cookie=a+"="+c+b+"; path=/; "};
      this.check=function(){var a=this.get(f);if(a)a=a.split(":");else if(100!=e)"v"==h&&(e=Math.random()>=e/100?0:100),a=[h,e,0],this.set(f,a.join(":"));else return!0;var c=a[1];if(100==c)return!0;switch(a[0]){case "v":return!1;case "r":return c=a[2]%Math.floor(100/c),a[2]++,this.set(f,a.join(":")),!c}return!0};
      this.go=function(){if(this.check()){var a=document.createElement("script");a.type="text/javascript";a.src=g;document.body&&document.body.appendChild(a)}};
      this.start=function(){var a=this;window.addEventListener?window.addEventListener("load",function(){a.go()},!1):window.attachEvent&&window.attachEvent("onload",function(){a.go()})}};
      try{(new g(100,"r","QSI_S_ZN_emkP0oSe9Qrn7kF","https://znemkp0ose9qrn7kf-elastic.siteintercept.qualtrics.com/WRSiteInterceptEngine/?Q_ZID=ZN_emkP0oSe9Qrn7kF")).start()}catch(i){}})();
    </script><div id="ZN_emkP0oSe9Qrn7kF"><!--DO NOT REMOVE-CONTENTS PLACED HERE--></div>
    <!--END WEBSITE FEEDBACK SNIPPET-->

    <div id="elastic-nav" style="display:none;"></div>
    <script src="https://www.elastic.co/elastic-nav.js"></script>

    <!-- Subnav -->
    <div>
      <div>
        <div class="tertiary-nav d-none d-md-block">
          <div class="container">
            <div class="p-t-b-15 d-flex justify-content-between nav-container">
              <div class="breadcrum-wrapper"><span><a href="/guide/" style="font-size: 14px; font-weight: 600; color: #000;">Docs</a></span></div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="main-container">
      <section id="content">
        <div class="content-wrapper">

          <section id="guide" lang="en">
            <div class="container">
              <div class="row">
                <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                  <!-- start body -->
                  <div class="page_header">
<strong>IMPORTANT</strong>: No additional bug fixes or documentation updates
will be released for this version. For the latest information, see the
<a href="../current/index.html">current release documentation</a>.
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations.html">Aggregations</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations-bucket.html">Bucket Aggregations</a></span>
»
<span class="breadcrumb-node">Rare Terms Aggregation</span>
</div>
<div class="navheader">
<span class="prev">
<a href="search-aggregations-bucket-range-aggregation.html">« Range Aggregation</a>
</span>
<span class="next">
<a href="search-aggregations-bucket-reverse-nested-aggregation.html">Reverse nested Aggregation »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="search-aggregations-bucket-rare-terms-aggregation"></a>Rare Terms Aggregation<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/aggregations/bucket/rare-terms-aggregation.asciidoc">edit</a>
</h2>
</div></div></div>
<p>A multi-bucket value source based aggregation which finds "rare" terms — terms that are at the long-tail
of the distribution and are not frequent.  Conceptually, this is like a <code class="literal">terms</code> aggregation that is
sorted by <code class="literal">_count</code> ascending.  As noted in the <a class="xref" href="search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-order" title="Order">terms aggregation docs</a>,
actually ordering a <code class="literal">terms</code> agg by count ascending has unbounded error.  Instead, you should use the <code class="literal">rare_terms</code>
aggregation</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_syntax_2"></a>Syntax<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/aggregations/bucket/rare-terms-aggregation.asciidoc">edit</a>
</h3>
</div></div></div>
<p>A <code class="literal">rare_terms</code> aggregation looks like this in isolation:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
    "rare_terms": {
        "field": "the_field",
        "max_doc_count": 1
    }
}</pre>
</div>
<div class="table">
<p class="title"><strong>Table 6. <code class="literal">rare_terms</code> Parameters</strong></p>
<div class="table-contents">
<table border="1" cellpadding="4px" summary="rare_terms Parameters">
<colgroup>
<col class="col_1">
<col class="col_2">
<col class="col_3">
<col class="col_4">
</colgroup>
<tbody>
<tr>
<td align="left" valign="top"><p>Parameter Name</p></td>
<td align="left" valign="top"><p>Description</p></td>
<td align="left" valign="top"><p>Required</p></td>
<td align="left" valign="top"><p>Default Value</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">field</code></p></td>
<td align="left" valign="top"><p>The field we wish to find rare terms in</p></td>
<td align="left" valign="top"><p>Required</p></td>
<td align="left" valign="top"><p></p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">max_doc_count</code></p></td>
<td align="left" valign="top"><p>The maximum number of documents a term should appear in.</p></td>
<td align="left" valign="top"><p>Optional</p></td>
<td align="left" valign="top"><p><code class="literal">1</code></p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">precision</code></p></td>
<td align="left" valign="top"><p>The precision of the internal CuckooFilters.  Smaller precision leads to
better approximation, but higher memory usage. Cannot be smaller than <code class="literal">0.00001</code></p></td>
<td align="left" valign="top"><p>Optional</p></td>
<td align="left" valign="top"><p><code class="literal">0.01</code></p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">include</code></p></td>
<td align="left" valign="top"><p>Terms that should be included in the aggregation</p></td>
<td align="left" valign="top"><p>Optional</p></td>
<td align="left" valign="top"><p></p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">exclude</code></p></td>
<td align="left" valign="top"><p>Terms that should be excluded from the aggregation</p></td>
<td align="left" valign="top"><p>Optional</p></td>
<td align="left" valign="top"><p></p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">missing</code></p></td>
<td align="left" valign="top"><p>The value that should be used if a document does not have the field being aggregated</p></td>
<td align="left" valign="top"><p>Optional</p></td>
<td align="left" valign="top"><p></p></td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Example:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /_search
{
    "aggs" : {
        "genres" : {
            "rare_terms" : {
                "field" : "genre"
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/502.console"></div>
<p>Response:</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
    ...
    "aggregations" : {
        "genres" : {
            "buckets" : [
                {
                    "key" : "swing",
                    "doc_count" : 1
                }
            ]
        }
    }
}</pre>
</div>
<p>In this example, the only bucket that we see is the "swing" bucket, because it is the only term that appears in
one document.  If we increase the <code class="literal">max_doc_count</code> to <code class="literal">2</code>, we’ll see some more buckets:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /_search
{
    "aggs" : {
        "genres" : {
            "rare_terms" : {
                "field" : "genre",
                "max_doc_count": 2
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/503.console"></div>
<p>This now shows the "jazz" term which has a <code class="literal">doc_count</code> of 2":</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
    ...
    "aggregations" : {
        "genres" : {
            "buckets" : [
                {
                    "key" : "swing",
                    "doc_count" : 1
                },
                {
                    "key" : "jazz",
                    "doc_count" : 2
                }
            ]
        }
    }
}</pre>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="search-aggregations-bucket-rare-terms-aggregation-max-doc-count"></a>Maximum document count<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/aggregations/bucket/rare-terms-aggregation.asciidoc">edit</a>
</h3>
</div></div></div>
<p>The <code class="literal">max_doc_count</code> parameter is used to control the upper bound of document counts that a term can have.  There
is not a size limitation on the <code class="literal">rare_terms</code> agg like <code class="literal">terms</code> agg has.  This means that terms
which match the <code class="literal">max_doc_count</code> criteria will be returned.  The aggregation functions in this manner to avoid
the order-by-ascending issues that afflict the <code class="literal">terms</code> aggregation.</p>
<p>This does, however, mean that  a large number of results can be returned if chosen incorrectly.
To limit the danger of this setting, the maximum <code class="literal">max_doc_count</code> is 100.</p>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="search-aggregations-bucket-rare-terms-aggregation-max-buckets"></a>Max Bucket Limit<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/aggregations/bucket/rare-terms-aggregation.asciidoc">edit</a>
</h3>
</div></div></div>
<p>The Rare Terms aggregation is more liable to trip the <code class="literal">search.max_buckets</code> soft limit than other aggregations due
to how it works.  The <code class="literal">max_bucket</code> soft-limit is evaluated on a per-shard basis while the aggregation is collecting
results.  It is possible for a term to be "rare" on a shard but become "not rare" once all the shard results are
merged together.  This means that individual shards tend to collect more buckets than are truly rare, because
they only have their own local view.  This list is ultimately pruned to the correct, smaller list of rare
terms on the coordinating node…​ but a shard may have already tripped the <code class="literal">max_buckets</code> soft limit and aborted
the request.</p>
<p>When aggregating on fields that have potentially many "rare" terms, you may need to increase the <code class="literal">max_buckets</code> soft
limit.  Alternatively, you might need to find a way to filter the results to return fewer rare values (smaller time
span, filter by category, etc), or re-evaluate your definition of "rare" (e.g. if something
appears 100,000 times, is it truly "rare"?)</p>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="search-aggregations-bucket-rare-terms-aggregation-approximate-counts"></a>Document counts are approximate<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/aggregations/bucket/rare-terms-aggregation.asciidoc">edit</a>
</h3>
</div></div></div>
<p>The naive way to determine the "rare" terms in a dataset is to place all the values in a map, incrementing counts
as each document is visited, then return the bottom <code class="literal">n</code> rows.  This does not scale beyond even modestly sized data
sets.  A sharded approach where only the "top n" values are retained from each shard (ala the <code class="literal">terms</code> aggregation)
fails because the long-tail nature of the problem means it is impossible to find the "top n" bottom values without
simply collecting all the values from all shards.</p>
<p>Instead, the Rare Terms aggregation uses a different approximate algorithm:</p>
<div class="olist orderedlist">
<ol class="orderedlist">
<li class="listitem">
Values are placed in a map the first time they are seen.
</li>
<li class="listitem">
Each addition occurrence of the term increments a counter in the map
</li>
<li class="listitem">
If the counter &gt; the <code class="literal">max_doc_count</code> threshold, the term is removed from the map and placed in a
<a href="https://www.cs.cmu.edu/~dga/papers/cuckoo-conext2014.pdf" class="ulink" target="_top">CuckooFilter</a>
</li>
<li class="listitem">
The CuckooFilter is consulted on each term.  If the value is inside the filter, it is known to be above the
threshold already and skipped.
</li>
</ol>
</div>
<p>After execution, the map of values is the map of "rare" terms under the <code class="literal">max_doc_count</code> threshold.  This map and CuckooFilter
are then merged with all other shards.  If there are terms that are greater than the threshold (or appear in
a different shard’s CuckooFilter) the term is removed from the merged list.  The final map of values is returned
to the user as the "rare" terms.</p>
<p>CuckooFilters have the possibility of returning false positives (they can say a value exists in their collection when
it actually does not).  Since the CuckooFilter is being used to see if a term is over threshold, this means a false positive
from the CuckooFilter will mistakenly say a value is common when it is not (and thus exclude it from it final list of buckets).
Practically, this means the aggregations exhibits false-negative behavior since the filter is being used "in reverse"
of how people generally think of approximate set membership sketches.</p>
<p>CuckooFilters are described in more detail in the paper:</p>
<p><a href="https://www.cs.cmu.edu/~dga/papers/cuckoo-conext2014.pdf" class="ulink" target="_top">Fan, Bin, et al. "Cuckoo filter: Practically better than bloom."</a>
Proceedings of the 10th ACM International on Conference on emerging Networking Experiments and Technologies. ACM, 2014.</p>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_precision"></a>Precision<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/aggregations/bucket/rare-terms-aggregation.asciidoc">edit</a>
</h3>
</div></div></div>
<p>Although the internal CuckooFilter is approximate in nature, the false-negative rate can be controlled with a
<code class="literal">precision</code> parameter.  This allows the user to trade more runtime memory for more accurate results.</p>
<p>The default precision is <code class="literal">0.001</code>, and the smallest (e.g. most accurate and largest memory overhead) is <code class="literal">0.00001</code>.
Below are some charts which demonstrate how the accuracy of the aggregation is affected by precision and number
of distinct terms.</p>
<p>The X-axis shows the number of distinct values the aggregation has seen, and the Y-axis shows the percent error.
Each line series represents one "rarity" condition (ranging from one rare item to 100,000 rare items).  For example,
the orange "10" line means ten of the values were "rare" (<code class="literal">doc_count == 1</code>), out of 1-20m distinct values (where the
rest of the values had <code class="literal">doc_count &gt; 1</code>)</p>
<p>This first chart shows precision <code class="literal">0.01</code>:</p>
<p><span class="image"><img src="images/rare_terms/accuracy_01.png" alt="accuracy 01"></span></p>
<p>And precision <code class="literal">0.001</code> (the default):</p>
<p><span class="image"><img src="images/rare_terms/accuracy_001.png" alt="accuracy 001"></span></p>
<p>And finally <code class="literal">precision 0.0001</code>:</p>
<p><span class="image"><img src="images/rare_terms/accuracy_0001.png" alt="accuracy 0001"></span></p>
<p>The default precision of <code class="literal">0.001</code> maintains an accuracy of &lt; 2.5% for the tested conditions, and accuracy slowly
degrades in a controlled, linear fashion as the number of distinct values increases.</p>
<p>The default precision of <code class="literal">0.001</code> has a memory profile of <code class="literal">1.748⁻⁶ * n</code> bytes, where <code class="literal">n</code> is the number
of distinct values the aggregation has seen (it can also be roughly eyeballed, e.g. 20 million unique values is about
30mb of memory).  The memory usage is linear to the number of distinct values regardless of which precision is chosen,
the precision only affects the slope of the memory profile as seen in this chart:</p>
<p><span class="image"><img src="images/rare_terms/memory.png" alt="memory"></span></p>
<p>For comparison, an equivalent terms aggregation at 20 million buckets would be roughly
<code class="literal">20m * 69b == ~1.38gb</code> (with 69 bytes being a very optimistic estimate of an empty bucket cost, far lower than what
the circuit breaker accounts for).  So although the <code class="literal">rare_terms</code> agg is relatively heavy, it is still orders of
magnitude smaller than the equivalent terms aggregation</p>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_filtering_values"></a>Filtering Values<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/aggregations/bucket/rare-terms-aggregation.asciidoc">edit</a>
</h3>
</div></div></div>
<p>It is possible to filter the values for which buckets will be created. This can be done using the <code class="literal">include</code> and
<code class="literal">exclude</code> parameters which are based on regular expression strings or arrays of exact values. Additionally,
<code class="literal">include</code> clauses can filter using <code class="literal">partition</code> expressions.</p>
<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="_filtering_values_with_regular_expressions"></a>Filtering Values with regular expressions<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/aggregations/bucket/rare-terms-aggregation.asciidoc">edit</a>
</h4>
</div></div></div>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /_search
{
    "aggs" : {
        "genres" : {
            "rare_terms" : {
                "field" : "genre",
                "include" : "swi*",
                "exclude" : "electro*"
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/504.console"></div>
<p>In the above example, buckets will be created for all the tags that starts with <code class="literal">swi</code>, except those starting
with <code class="literal">electro</code> (so the tag <code class="literal">swing</code> will be aggregated but not <code class="literal">electro_swing</code>). The <code class="literal">include</code> regular expression will determine what
values are "allowed" to be aggregated, while the <code class="literal">exclude</code> determines the values that should not be aggregated. When
both are defined, the <code class="literal">exclude</code> has precedence, meaning, the <code class="literal">include</code> is evaluated first and only then the <code class="literal">exclude</code>.</p>
<p>The syntax is the same as <a class="xref" href="regexp-syntax.html" title="Regular expression syntax">regexp queries</a>.</p>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="_filtering_values_with_exact_values"></a>Filtering Values with exact values<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/aggregations/bucket/rare-terms-aggregation.asciidoc">edit</a>
</h4>
</div></div></div>
<p>For matching based on exact values the <code class="literal">include</code> and <code class="literal">exclude</code> parameters can simply take an array of
strings that represent the terms as they are found in the index:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /_search
{
    "aggs" : {
        "genres" : {
             "rare_terms" : {
                 "field" : "genre",
                 "include" : ["swing", "rock"],
                 "exclude" : ["jazz"]
             }
         }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/505.console"></div>
</div>

</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_missing_value_16"></a>Missing value<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/aggregations/bucket/rare-terms-aggregation.asciidoc">edit</a>
</h3>
</div></div></div>
<p>The <code class="literal">missing</code> parameter defines how documents that are missing a value should be treated.
By default they will be ignored but it is also possible to treat them as if they
had a value.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /_search
{
    "aggs" : {
        "genres" : {
             "rare_terms" : {
                 "field" : "genre",
                 "missing": "N/A" <a id="CO237-1"></a><i class="conum" data-value="1"></i>
             }
         }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/506.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO237-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>Documents without a value in the <code class="literal">tags</code> field will fall into the same bucket as documents that have the value <code class="literal">N/A</code>.</p>
</td>
</tr>
</table>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_nested_rareterms_and_scoring_sub_aggregations"></a>Nested, RareTerms, and scoring sub-aggregations<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/aggregations/bucket/rare-terms-aggregation.asciidoc">edit</a>
</h3>
</div></div></div>
<p>The RareTerms aggregation has to operate in <code class="literal">breadth_first</code> mode, since it needs to prune terms as doc count thresholds
are breached.  This requirement means the RareTerms aggregation is incompatible with certain combinations of aggregations
that require <code class="literal">depth_first</code>. In particular, scoring sub-aggregations that are inside a <code class="literal">nested</code> force the entire aggregation tree to run
in <code class="literal">depth_first</code> mode.  This will throw an exception since RareTerms is unable to process <code class="literal">depth_first</code>.</p>
<p>As a concrete example, if <code class="literal">rare_terms</code> aggregation is the child of a <code class="literal">nested</code> aggregation, and one of the child aggregations of <code class="literal">rare_terms</code>
needs document scores (like a <code class="literal">top_hits</code> aggregation), this will throw an exception.</p>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="search-aggregations-bucket-range-aggregation.html">« Range Aggregation</a>
</span>
<span class="next">
<a href="search-aggregations-bucket-reverse-nested-aggregation.html">Reverse nested Aggregation »</a>
</span>
</div>
</div>

                  <!-- end body -->
                </div>
                <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                  <div id="rtpcontainer" style="display: block;">
                    <div class="mktg-promo">
                      <h3>Most Popular</h3>
                      <ul class="icons">
                        <li class="icon-elasticsearch-white"><a href="https://www.elastic.co/webinars/getting-started-elasticsearch?baymax=default&amp;elektra=docs&amp;storm=top-video">Get Started with Elasticsearch: Video</a></li>
                        <li class="icon-kibana-white"><a href="https://www.elastic.co/webinars/getting-started-kibana?baymax=default&amp;elektra=docs&amp;storm=top-video">Intro to Kibana: Video</a></li>
                        <li class="icon-logstash-white"><a href="https://www.elastic.co/webinars/introduction-elk-stack?baymax=default&amp;elektra=docs&amp;storm=top-video">ELK for Logs &amp; Metrics: Video</a></li>
                      </ul>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </section>

        </div>


<div id="elastic-footer"></div>
<script src="https://www.elastic.co/elastic-footer.js"></script>
<!-- Footer Section end-->

      </section>
    </div>

<script src="/guide/static/jquery.js"></script>
<script type="text/javascript" src="/guide/static/docs.js"></script>
<script type="text/javascript">
  window.initial_state = {}</script>
  </body>
</html>
